package com.example.springbootdemo09.controller;

import com.example.springbootdemo09.domain.Student;
import com.example.springbootdemo09.query.StudentQuery;
import com.example.springbootdemo09.service.IStudentService;
import com.example.springbootdemo09.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService igs;
    //查询所有

    /**
     * 查询
     * @return
     */
    @GetMapping
    R findAll(){
        List<Student> all = igs.findAll();
        System.out.println(R.ok(all));
        return R.ok(all);
//        return igs.findAll();
    }
    //查询单个
    /**
     * 查询单个
     * @return
     */
    @GetMapping("{id}")
    R findOne(@PathVariable("id") Long id){
        Student one = igs.findOne(id);
        return R.ok(one);
    }
    //修改
    /**
     * 修改
     * @return
     */
    @PutMapping
    R updete(@RequestBody Student student){
        if (student.getId()!=null){
            igs.updete(student);
        }else {
            igs.add(student);
        }
        return R.ok();
    }
    //删除
    /**
     * 删除
     * @return
     */
    @DeleteMapping("{id}")
    R delete(@PathVariable("id") Long id){
        try{
            igs.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }

    }
    /**
     * 批量删除
     * @return
     */
@PatchMapping
    //批量删除
    R PathDelete(@RequestBody Long[]id){
    igs.pathDele(id);
    return  R.ok();
    }
    //APS-gsoRcWKwhPcLsjXhe2YNJ0jxsw8vHCVZ
    //3976915

    @PostMapping
    //高级分页查询
    R QueryFenye(@RequestBody StudentQuery studentQuery){
        R fenye = igs.fenye(studentQuery);
        return  R.ok(fenye);
    }

}
